package com.szxy.sso.controller;

import com.szxy.pojo.TbUser;
import com.szxy.sso.service.SSOService;
import com.szxy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther:zwer
 * @Date:2019/9/12 15:19
 * @Description:com.szxy.sso.controller
 * @Version:1.0
 **/
@RestController
@RequestMapping("/sso")
public class SSOController {

    @Autowired
    private SSOService ssoService;

    /**
     * 校验用户信息
     * @param checkValue  需要被校验的 value
     * @param checkFlag   1. 表示校验用户名  2. 表示校验手机名是否存在
     * @return
     */
    @RequestMapping("/checkUserInfo/{checkValue}/{checkFlag}")
    public Result checkUserInfo(@PathVariable String checkValue, @PathVariable int checkFlag){
        try {
            return this.ssoService.checkUserInfo(checkValue,checkFlag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(500,"error");
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @RequestMapping("/userRegister")
    public Result userRegister(TbUser user){
        try {
            return this.ssoService.userRegister(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(500,"error");
    }

    /**
     * 用户登录
     * @param user
     * @return
     */
    @RequestMapping(value="/userLogin")
    public Result userLogin(TbUser user, HttpServletRequest request, HttpServletResponse response){
        try {
            return this.ssoService.userLogin(user,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(500,"error");
    }

    /**
     * 用户退出
     * @param token
     * @return
     */
    @RequestMapping(value="/logOut")
    public Result logOut(String token,HttpServletRequest request,HttpServletResponse response){
        try {
            return this.ssoService.logOut(token,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(500,"error");
    }

}
